﻿using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Resources;
using System.Runtime.Remoting.Contexts;
using Moq;
using NUnit.Framework;

namespace UnitTest
{
    [TestFixture]
    public class Program
    {
        //public Program()
        //{
            
        //}

        [TestFixtureSetUp]
        public void Init()
        {
            // do stuff
        }

        [SetUp]
        public void Setup()
        {

        }

        [Test]
        public void TestMethod()
        {
        }

        [TearDown]
        public void TearDown()
        {

        }

        [TestFixtureTearDown]
        public void Cleanup()
        {
        }

        static void Main(string[] args)
        {
        }
    }

    public interface IMappingEngine
    {
    }

    public interface IResourceManager
    {
    }


    public static class DbConnectionFactory
    {
        public static DbConnection CreateTransient()
        {
            return new SqlConnection();
        }
    }

    public class MContext
    {
    }
    
    public class BaseHandlerTest
    {
        protected IMappingEngine Mapper;

        [SetUp]
        protected virtual void SetUp()
        {
            AutoMapper.Mapper.Initialize(c =>
            {
                //c.AddProfile<EntityToDomainObjMappingProfile>();
                //c.AddProfile<CommandToEntityMappingProfile>();
            });
            //Mapper = AutoMapper.Mapper.Engine;
        }

        [TearDown]
        protected virtual void TearDown()
        {
            AutoMapper.Mapper.Reset();
        }

        protected virtual DbConnection GetTransientConnection()
        {
            return DbConnectionFactory.CreateTransient();
        }

        protected virtual DbConnection GetTransientConnectionWithDataLoader()
        {
            return DbConnectionFactory.CreateTransient();
            //return DbConnectionFactory.CreateTransient(Initialize.DataLoader);
        }

        protected virtual DbConnection GetTransientConnectionWithDataLoader(string dirPath)
        {
            return DbConnectionFactory.CreateTransient();
            //return DbConnectionFactory.CreateTransient(Initialize.GetDataLoader(dirPath));
        }

        protected void AssertPropertiesEqual<TExpected, TActual>(TExpected expected, TActual actual, string[] excludedProperties = null)
        {
            foreach (var propInfo in actual.GetType().GetProperties()
                    .Where(propInfo => propInfo.CanRead && (excludedProperties == null || !excludedProperties.Contains(propInfo.Name))))
            {
                var expectedPropInfo = expected.GetType().GetProperty(propInfo.Name);
                var expectedValue = expectedPropInfo.GetValue(expected);
                var actualValue = propInfo.GetValue(actual);
                Assert.That(actualValue, Is.EqualTo(expectedValue));
            }
        }
    }

    public class BaseQueryHandlerTest : BaseHandlerTest
    {
        protected Context DbContext;

        [SetUp]
        protected override void SetUp()
        {
            base.SetUp();
            //DbContext = TestsSetup.DbContext;
        }

        [TearDown]
        protected override void TearDown()
        {
            base.TearDown();
            DbContext = null;
        }
    }

    public class BaseMHandlerTest : BaseHandlerTest
    {
        protected IDataAdapter DataAdapter;
        //protected IResourceManager ResourceManager;
        //protected ISequenceFactory SequenceFactory;
        //protected IClaimHelper ClaimHelper;

        [SetUp]
        protected override void SetUp()
        {
            AutoMapper.Mapper.Initialize(c =>
            {
                //c.AddProfile<EntityToDomainObjMappingProfile>();
                //c.AddProfile<CommandToEntityMappingProfile>();
            });

            //SequenceFactory = Mock.Of<ISequenceFactory>();
            //Mapper = AutoMapper.Mapper.Engine;
            //ResourceManager = new ResourceManager();
            DataAdapter = Mock.Of<IDataAdapter>();

            //PaymentApprovalService = Mock.Of<IPaymentApprovalService>();
            //Mock.Get(DataAdapter)
            //    .Setup(d => d.GetExchangeRates(It.IsAny<string[]>(), It.IsAny<string>()))
            //    .Returns(new List<CurrencyExchangeRateViewModel>
            //             {
            //                 new CurrencyExchangeRateViewModel
            //                 {
            //                     CurrencyCode = "USD",
            //                     CurrencyRate = 1,
            //                     ExchangeCurrencyCode = "GBP"
            //                 },
            //                 new CurrencyExchangeRateViewModel
            //                 {
            //                     CurrencyCode = "USD",
            //                     CurrencyRate = 1,
            //                     ExchangeCurrencyCode = "AUD"
            //                 },
            //                 new CurrencyExchangeRateViewModel
            //                 {
            //                     CurrencyCode = "USD",
            //                     CurrencyRate = 1,
            //                     ExchangeCurrencyCode = "EUR"
            //                 }, new CurrencyExchangeRateViewModel
            //                 {
            //                     CurrencyCode = "USD",
            //                     CurrencyRate = 1,
            //                     ExchangeCurrencyCode = "SGD"
            //                 }, new CurrencyExchangeRateViewModel
            //                 {
            //                     CurrencyCode = "USD",
            //                     CurrencyRate = 1,
            //                     ExchangeCurrencyCode = "USD"
            //                 }
            //             });
        }

        protected override DbConnection GetTransientConnectionWithDataLoader()
        {
            return DbConnectionFactory.CreateTransient();
            //return DbConnectionFactory.CreateTransient(Initialize.DataLoader.WrappedDataLoader);
        }

        [TearDown]
        protected virtual void Teardown()
        {
            //MemoryCache.Default.Remove(typeof(ApprovalPath).FullName);
        }
    }

    public class BaseMQueryHandlerTest : BaseMHandlerTest
    {
        protected MContext DbContext;

        [SetUp]
        protected override void SetUp()
        {
            base.SetUp();
            //DbContext = MTestsSetup.DbContext;
        }

        [TearDown]
        protected override void TearDown()
        {
            base.TearDown();
            DbContext = null;
        }
    }

    [TestFixture]
    public class RemoveSectionHandlerTest : BaseMQueryHandlerTest
    {
        private IResourceManager resourceManager;

        public RemoveSectionHandlerTest()
        {

        }

        //[SetUp]
        //protected override void SetUp()
        //{
        //    base.SetUp();
        //    var container = new ContainerBuilder();
        //    container.RegisterType<CoreResourceManager>().As<IResourceManager>();
        //    DependencyResolver.SetResolver(new AutofacDependencyResolver(container.Build()));
        //    new HttpSimulator().SimulateRequest();
        //    resourceManager = DependencyResolver.Current.GetService<IResourceManager>();

        //}

        //[Test]
        //[TestCase(null)]
        //[TestCase(0)]
        //[TestCase(1000)]
        //public void GetEmptyResponseIfCommandIsNotValid(int? sectionId)
        //{
        //    using (var dbContext = new MContext(GetTransientConnection()))
        //    {
        //        dbContext.Database.CreateIfNotExists();

        //        var handler = new RemoveSectionHandler(dbContext, Mapper, resourceManager);
        //        var response =
        //            handler.Handle(sectionId.HasValue ? new RemoveSectionCommand { Id = sectionId.Value } : null);

        //        Assert.IsNotNull(response);
        //        Assert.That(response.ClaimId == 0);
        //        Assert.That(response.SectionId == 0);
        //        Assert.IsNotNull(response.Message);
        //        Assert.That(response.Message, Is.Not.Null);
        //    }
        //}


        //[Test]
        //[TestCase(40)]
        //public void GetARuleWizardViewModelWithClaimId(int claimId)
        //{
        //    var query = new GetRuleWizardQuery
        //    {
        //        ClaimId = claimId
        //    };
        //    var getRuleWizardHandler = new GetRuleWizardHandler(DbContext, new MService(DbContext));

        //    RuleWizardViewModel response = getRuleWizardHandler.Handle(query);

        //    Assert.That(response, Is.Not.Null);
        //    Assert.AreEqual(claimId, response.ClaimId);
        //    Assert.AreEqual("0040", response.ClaimNumber);
        //}        //[SetUp]
        //protected override void SetUp()
        //{
        //    base.SetUp();
        //    var container = new ContainerBuilder();
        //    container.RegisterType<CoreResourceManager>().As<IResourceManager>();
        //    DependencyResolver.SetResolver(new AutofacDependencyResolver(container.Build()));
        //    new HttpSimulator().SimulateRequest();
        //    resourceManager = DependencyResolver.Current.GetService<IResourceManager>();

        //}

        //[Test]
        //[TestCase(null)]
        //[TestCase(0)]
        //[TestCase(1000)]
        //public void GetEmptyResponseIfCommandIsNotValid(int? sectionId)
        //{
        //    using (var dbContext = new MContext(GetTransientConnection()))
        //    {
        //        dbContext.Database.CreateIfNotExists();

        //        var handler = new RemoveSectionHandler(dbContext, Mapper, resourceManager);
        //        var response =
        //            handler.Handle(sectionId.HasValue ? new RemoveSectionCommand { Id = sectionId.Value } : null);

        //        Assert.IsNotNull(response);
        //        Assert.That(response.ClaimId == 0);
        //        Assert.That(response.SectionId == 0);
        //        Assert.IsNotNull(response.Message);
        //        Assert.That(response.Message, Is.Not.Null);
        //    }
        //}


        //[Test]
        //[TestCase(40)]
        //public void GetARuleWizardViewModelWithClaimId(int claimId)
        //{
        //    var query = new GetRuleWizardQuery
        //    {
        //        ClaimId = claimId
        //    };
        //    var getRuleWizardHandler = new GetRuleWizardHandler(DbContext, new MService(DbContext));

        //    RuleWizardViewModel response = getRuleWizardHandler.Handle(query);

        //    Assert.That(response, Is.Not.Null);
        //    Assert.AreEqual(claimId, response.ClaimId);
        //    Assert.AreEqual("0040", response.ClaimNumber);
        //}
    }
}
